<template>
  <el-card>
    <el-button type="primary" icon="plus" @click="addNotice"
      >添加公告</el-button
    >
  </el-card>
  <el-card style="margin: 10px 0px">
    <el-table :data="notaceList" border style="margin-bottom: 10px">
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        label="标题"
        align="center"
        prop="title"
      ></el-table-column>
      <el-table-column label="发布者" align="center">
        <template v-slot="{ row }">
          {{ row.docId?.name }}
        </template>
      </el-table-column>
      <el-table-column label="所属科室" align="center">
        <template #default="{ row }">
          {{ row.docId?.TweDepart }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template #default="{ row }">
          {{ formatDate(row.createAt) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            type="info"
            size="small"
            icon="edit"
            @click="updateNotice(row._id)"
            >编辑</el-button
          >
          <el-popconfirm :title="`你确定删除${row.title}吗?`" @confirm="deleteNotice(row._id)" >
            <template #reference>
              <el-button type="danger" size="small" icon="delete">删除</el-button>
            </template>
          </el-popconfirm>
        
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      background
      layout="jumper,prev, pager, next, total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <el-dialog
    v-model="centerDialogVisible"
    :title="callParmas.id ? '修改公告' : '添加公告'"
    width="1200"
    :close-on-click-modal="false"
    draggable
    overflow
    @open="handleOpen"
    :z-index="10"
  >
    <el-form :model="callParmas" label-width="120px" ref="form" :rules="rules">
      <el-form-item label="标题:" style="width: 50%" prop="title">
        <el-input
          v-model="callParmas.title"
          placeholder="请输入标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="内容:" prop="content">
        <div id="mytin">
          <tinymces
            v-model="callParmas.content"
            ref="tinmce"
            :init="options"
            api-key="vwjzouj8jihgco1lwdzfa8cbks5udysq0ce9jcvxweccfjpn"
          ></tinymces>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="AddOrUpdateNotice"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, onMounted, nextTick } from "vue";
import {
  getNoticeListAPI,
  CreateOrUpdateNoticeAPI,
  getNoticeDetailAPI,
  deleteNoticeAPI
} from "@/api/call";
import { ElMessage } from "element-plus";
import tinymces from "@tinymce/tinymce-vue";
let notaceList = ref([]);
let total = ref(0);
let pageNo = ref(1);
let pageSize = ref(10);
let options = reactive({
  language: "zh_CN",
  apiKey: "vwjzouj8jihgco1lwdzfa8cbks5udysq0ce9jcvxweccfjpn",
  toolbar: "undo redo | styles | bold italic | fontsize fontfamily forecolor",
  width: 800,
});
let tinmce = ref();
let callParmas = reactive({
  title: "",
  content: "",
});
let rules = reactive({
  title: [
    { required: true, message: "请输入标题", trigger: "blur" },
    { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" },
  ],
  content: [{ required: true, message: "请输入内容", trigger: "blur" }],
});
let form = ref(null);
let centerDialogVisible = ref(false);
onMounted(() => {
  getNoticeList();
});

//获取公告列表
const getNoticeList = async () => {
  let result = await getNoticeListAPI(pageNo.value, pageSize.value);
  notaceList.value = result.data;
  total.value = result.total;
};
const handleSizeChange = () => {
  getNoticeList();
};
const handleCurrentChange = () => {
  getNoticeList();
};
const deleteNotice = async (id)=>{
  await deleteNoticeAPI(id)
  ElMessage.success('删除成功')
  getNoticeList()
}
//对话框开启的回调
const handleOpen = () => {
  callParmas.title = "";
  callParmas.content = "";
  callParmas.id = "";
  //重置
  form.value.resetFields();
};
const addNotice = () => {
  centerDialogVisible.value = true;

};
//添加或者修改公告
const AddOrUpdateNotice = async () => {
  await form.value.validate();
  await CreateOrUpdateNoticeAPI(callParmas);
  ElMessage.success(callParmas.id ? "修改成功" : "添加成功");
  centerDialogVisible.value = false;
  // editorRef.value.destroy()
  getNoticeList();
};
//修改公告的回调
const updateNotice = async (id) => {
  centerDialogVisible.value = true;
  callParmas.id = id;
  let result = await getNoticeDetailAPI(id);

  callParmas.title = result.data.title;
  callParmas.content = result.data.content;
  callParmas.id = result.data._id;
  nextTick(() => {});
};
//格式化时间
function formatDate(isoString) {
  const date = new Date(isoString);
  const year = date.getUTCFullYear();
  const month = String(date.getUTCMonth() + 1).padStart(2, "0");
  const day = String(date.getUTCDate()).padStart(2, "0");

  return `${year}年${month}月${day}日`;
}
</script>
<script setup></script>

<style lang="scss" scoped>
.pagination {
  display: flex;
  justify-content: center;
}
</style>
